package com.bwie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bwie.utils.R;
import com.bwie.utils.TokenUtil;
import com.bwie.vo.PageInfoVo;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.GroupActive;
import com.bwie.mapper.GroupActiveMapper;
import com.bwie.service.GroupActiveService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @Author: LiuWeiChen
 * @Date:  2025/7/26 11:58
 * @Description: 
 **/
@Service
@Transactional
public class GroupActiveServiceImpl extends ServiceImpl<GroupActiveMapper, GroupActive> implements GroupActiveService{
    @Autowired
    private GroupActiveMapper activeMapper;
    @Autowired
    private GroupActiveMapper groupActiveMapper;
    @Autowired
    private HttpServletRequest request;

    @Override
    public R selectGroupActive(PageInfoVo pageInfoVo) {
        Page<GroupActive> page = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());

//        QueryWrapper<GroupActive> wrapper = new QueryWrapper<>();
//        if (StringUtils.isNotEmpty(pageInfoVo.getKeyWord())){
//            wrapper.lambda().like(GroupActive::getActiveName, pageInfoVo.getKeyWord())''
//        }

        Page<GroupActive> activeList = activeMapper.selectGroupActive(page,pageInfoVo);
        return R.OK(activeList);
    }

    @Override
    public R updateGroupActive(GroupActive groupActive) {
        //判断是否存在
        GroupActive one = groupActiveMapper.selectById(groupActive.getActiveId());
        if (ObjectUtils.isEmpty(one)){
            return R.ERROR(500,"该活动已不存在");
        }

        // 判断活动名称是否唯一
        if(!groupActive.getActiveName().equals(one.getActiveName())) {
            GroupActive groupActiveOne = groupActiveMapper.selectOne(
                    new LambdaQueryWrapper<GroupActive>().eq(GroupActive::getActiveName, groupActive.getActiveName())
            );
            if (ObjectUtils.isNotEmpty(groupActiveOne)){
                return R.ERROR(500,"该活动名称已存在,请勿重复添加");
            }
        }

        // 更新审计字段
        String userName = TokenUtil.getUserName(request.getHeader("token"));
        groupActive.setUpdateBy(userName);
        groupActive.setUpdateTime(new Date());

        //修改数据
        groupActiveMapper.updateById(groupActive);
        return R.OK();
    }

    @Override
    public R addGroupActive(GroupActive groupActive) {
        //检查活动名称是否存在
        GroupActive one = groupActiveMapper.selectOne(
                new LambdaQueryWrapper<GroupActive>().eq(GroupActive::getActiveName, groupActive.getActiveName())
        );
        if (ObjectUtils.isNotEmpty(one)){
            return R.ERROR(500,"该活动名称已存在,无法添加");
        }

        // 审计字段
        String userName = TokenUtil.getUserName(request.getHeader("token"));
        groupActive.setCreateBy(userName);
        groupActive.setCreateTime(new Date());

        //添加活动
        groupActiveMapper.insert(groupActive);
        return R.OK();
    }

    @Override
    public R deleteGroupActive(GroupActive groupActive) {
        GroupActive one = groupActiveMapper.selectById(groupActive.getActiveId());
        if (ObjectUtils.isEmpty(one)){
            return R.ERROR(500,"该活动已不存在");
        }
        //删除拼团活动
        groupActiveMapper.deleteById(groupActive);
        return R.OK();
    }
}
